<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="110px"
    >
      <el-form-item label="工序名称" prop="dictProcessName">
        <el-select
          v-model="queryParams.dictProcessName"
          placeholder="请选择工序名称"
          clearable
          class="!w-220px"
        >
          <el-option
            v-for="dict in getIntDictOptions('craft_process_name')"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="图号/编号" prop="drawingNumber">
        <el-input
          v-model="queryParams.drawingNumber"
          placeholder="请输入图号/编号"
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="单位" prop="dictValuationUnit">
        <el-select
          v-model="queryParams.dictValuationUnit"
          placeholder="请选择单位"
          clearable
          class="!w-220px"
        >
          <el-option
            v-for="dict in getIntDictOptions('config_wage_valuation_unit')"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="dictProcessValuationStatus">
        <el-select
          v-model="queryParams.dictProcessValuationStatus"
          placeholder="状态"
          clearable
          class="!w-220px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.CONFIG_WAGE_PROCESS_VALUATION_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="更新人" prop="updaterName">
        <el-input
          v-model="queryParams.updaterName"
          placeholder="请输入更新人"
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime">
        <el-date-picker
          v-model="queryParams.updateTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="setSearchParams(queryParams)">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <!-- 表单 -->
  <ContentWrap>
    <div style="width: 100%; height: 50px; display: flex; justify-content: space-between">
      <div>
        <el-button
          type="primary"
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['config:wage-process-valuation-version:export']"
        >
        数据导出
      </el-button>
      </div>
      <div>
          <el-button
            type="primary"
            @click="openTableTitleSet"
          >
          表头设置
          </el-button>
        </div>
      </div>
    <Table
      :columns="tableTitleList"
      :selection="tableObject.tableList.length ? true : false"
      @register="tableoperation"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      :tableHeight="tableHeight"
      >
      <template #action="{ row }">
        <el-button
          plain
          link
          type="primary"
          @click="openForm('details', row.id)"
          >
            查看
        </el-button>
      </template>
    </Table>
  </ContentWrap>
  <!-- 表头设置 -->
  <table-width-set ref="tableTitleSet" @refresh="getLabelAll" />
  <!-- 导出 -->
  <ExportFile ref="exportRef" @download="downloadFun" />
</template> 

<script setup name="WageProcessValuationVersion">
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import download from '@/utils/download'
import * as WageProcessValuationVersionApi from '@/api/config/wageProcessValuationVersion'
let  tableTitleList = ref([]) // 表头的数据
const loading = ref(true) // 列表的加载中
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  dictProcessName: null,
  drawingNumber: null,
  dictValuationUnit: null,
  dictProcessValuationStatus: null,
  updateTime: [],
  updaterName: null
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中



/** 查询列表 */
const { tableObject, tableMethods } = useTable({
    getListApi:WageProcessValuationVersionApi.getWageProcessValuationVersionPage, // 分页接口
    defaultParams: queryParams
  })

// 获得表格的各种操作
const { getList, setSearchParams } = tableMethods

/**表头数据 */
const getLabelAll = async () => {
  loading.value = true
  try {
    const res = await WageProcessValuationVersionApi.getWageProcessValuationVersionPage(queryParams)
    res.labels.push({
      label: '操作',
        field: 'action',
        isDetail: false,
        width: 80
    })
    const {allSchemas} = useCrudSchemas(res.labels)
    tableTitleList.value = allSchemas.tableColumns;
    /**表头修改需要刷新表格 */
    getList()
  } finally {
    loading.value = false
  }
}
/**表头内部方法 */
const usTableoperation = ref()
const tableoperation = (parent) => {
  usTableoperation.value = parent
}

 /**打开表格表头设置**/
 const tableTitleSet = ref() 
const openTableTitleSet = () => {
  const arr = tableTitleList.value.slice(0,-1)
  let code = 'WageProcessValuationVersion'
  tableTitleSet.value.open(arr, code)
}


/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

  /** 导出按钮操作 */
const exportRef = ref()
const handleExport = () => {
  exportRef.value.open()
}

  //清空多选
const clearSelectData = () => {
     usTableoperation.value.selections = []
     usTableoperation.value.clearTableSelect()
  }

// 导出数据
const downloadFun = async () => {
  const val = exportRef.value.params
  let params = {
    fileName: val.name,
    ids: ''
  }
  if (val.range === 'all') {
    params.ids = []
  } else {
    params.ids = usTableoperation.value.selections.map(item => item.id)
  }
    WageProcessValuationVersionApi.exportWageProcessValuationVersion(params).then(res => {
    download.excel(res, val.name)
    clearSelectData()
  })
}
/** 添加/修改操作 */
const { push } = useRouter() // 路由
const openForm = (type,id) => {
  push({
    path:'/config/config/salaryAssessment/wageProcessValuationVersionDetail',
    query: {
      type,
      id
    }
  })
}

/** 初始化 **/
const tableHeight = ref(0)
onMounted(async () => {
  const queryParamsHeight = queryFormRef.value.$el.clientHeight + 2
     tableHeight.value = document.body.clientHeight - 132 - queryParamsHeight - 127
  await getLabelAll()
  getList()
})
</script>
